﻿using MP_Game.Protos.V1;
using MP_Game.Services.V1;
using MP_Network;
using System.Net.Sockets;

namespace MP_Server.Ctrls
{
    public class ForwardCtrl : IContainer
    {
        /// <summary>
        /// 路由表，指明各个指令类型转发到哪个服务器
        /// </summary>
        private Dictionary<CmdId, CliType> _routeTable;
        private readonly ForwardSvc _forwardSvc;
        public ForwardCtrl(ForwardSvc forwardSvc)
        {
            //OnInit();
            _forwardSvc = forwardSvc;
        }
        public void OnInit()
        {
            _routeTable = new()
            {
                {CmdId.CmdLoginReq, CliType.VerifyServer },
                {CmdId.CmdLoginRsp, CliType.Player },
            };
        }
        public async void OnServerCommand(ServerBase serverBase, GamePacket packet)
        {
            // 转发
            await _forwardSvc.ForwardPacket(packet, null!);
            //_grpcCli.ForwardPacket(packet);
            //switch (packet.CmdId)
            //{
            //    case CmdId.CmdGatewayBroadcastReq:
            //        Broadcast(packet);
            //        break;
            //    default:
            //        ForwardSingle(packet);
            //        break;
            //}
        }
        public void OnClientCommand(ServerBase serverBase, GamePacket packet)
        {
            LogMsg.Warn("收到回应消息");
        }

        /// <summary>
        /// 转发给单个客户端
        /// </summary>
        /// <param name="packet"></param>
        private void ForwardSingle(GamePacket packet)
        {
            //_grpcCli.ForwardPacket(packet);
            //CmdId cmdId = packet.CmdId;
            //CliType des = _routeTable[cmdId];
            //LogMsg.Warn($"查询路由表得，{cmdId}消息即将转发给{des}，消息体：{packet}");
            //Session? desSession;
            //if (des != CliType.Player) // 对内转发，使用gRPC
            //{

            //    //desSession = SessionMgr.Instance.GetClient(des);
            //}
            //else
            //{
            //    desSession = SessionMgr.Instance.GetSession(packet.SessionId);
            //    if (desSession != null)
            //    {
            //        desSession.Send(packet);
            //    }
            //    else
            //    {
            //        LogMsg.Error($"ForwardCtrl::OnServerCommand::对外转发时{des}未连接！");
            //    }
            //}

            //if (desSession != null)
            //{
            //    desSession.Send(packet);
            //}
            //else
            //{
            //    LogMsg.Error($"ForwardCtrl::OnServerCommand::转发时{des}未连接！");
            //}
        }
        /// <summary>
        /// 转发给多个客户端
        /// </summary>
        /// <param name="packet"></param>
        private void Broadcast(GamePacket packet)
        {
            // 转发给多个目标客户端
            var bc = GatewayBroadcastReq.Parser.ParseFrom(packet.Data);
            foreach (var sid in bc.SessionIds)
            {
                Session? des = SessionMgr.Instance.GetSession(sid);
                if (des != null)
                {
                    des.Send(bc.Payload);
                }
                else
                {
                    LogMsg.Warn($"ForwardCtrl::Broadcast::消息准备推送给sid={sid}，但其会话已不存在！");
                }
            }
        }
    }
}
